<script lang="ts" setup>
import * as tenant from '@/api/tenant'
import type { TenaneQueryCriteria, TenantTableView } from '@/api/tenant/types'
import FormDialog from './form-dialog.vue'

const drawerVisible = ref(false)
const windowProp = ref<ModalWindowProp>({
    title: '租户管理'
})

const open = async (props: ModalWindowProp) => {
    windowProp.value = props
 
    await query()
    drawerVisible.value = true
}
defineExpose({
    open
})

const paginationRequest = reactive<PaginationRequest<TenaneQueryCriteria>>({
    pageIndex: 1,
    pageSize: 10,
    criteria: {
        keyword: '',
    },
    order: {
        property: 'id',
        direction: 'DESC'
    }
})
const queryCriteria = paginationRequest.criteria

const paginationResponse = reactive<PaginationResponse<TenantTableView>>({
    total: 0,
    data: []
})

const paginationRef = ref()
const query = async () => {
    const res = await tenant.query(paginationRequest)
    paginationResponse.total = res.total
    paginationResponse.data = res.data
    console.log(paginationResponse)
}

const currentPageChange = (pageIndex: number) => {
    console.log('currentPageChange', pageIndex)
    console.log('paginationRequest', paginationRequest)
    paginationRequest.pageIndex = pageIndex
    query()
}

const formDialogRef = ref()
const openFormDialog = (id?: string) => {
    if(id === undefined) {
        formDialogRef.value.open({
            title: '添加租户'
        })
    } else {
        formDialogRef.value.open({
            title: '编辑租户',
            context: id
        })
    }
}

onBeforeMount(() => {
    query()
})
</script>

<template>
    <el-drawer v-model="drawerVisible" :title="windowProp.title" size="50%">
        <el-form :inline="true" :model="queryCriteria" label-width="80px" style="text-align: left;">
            <el-form-item label="关键字">
                <el-input v-model="queryCriteria!.keyword" placeholder="租户名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="query()">查询</el-button>
            </el-form-item>
        </el-form>
        <el-divider />
        <div style="margin: 15px;">
            <div style="text-align: left; margin-left: 10px; margin-bottom: 15px;">
            <el-button type="primary" @click="openFormDialog()">添加</el-button>
            </div>
            <el-table :data="paginationResponse.data">
                <el-table-column prop="name" label="租户名称"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="openFormDialog(row.id)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination ref="paginationRef" :total="paginationResponse.total" 
                v-model:page-size="paginationRequest.pageSize" @size-change="query"
                v-model:current-page="paginationRequest.pageIndex" @current-change="currentPageChange"
                layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
        </div>
    </el-drawer>
    <FormDialog ref="formDialogRef" @close="query"></FormDialog>
</template>

<style lang="scss" scoped>

</style>